package com.aimee.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 利用反射执行多种方法
 */

public class BaseServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 你想执行的方法
		String m = req.getParameter("m");// m为方法名
		// 当前对象--方法
		try {
			// 使用反射
			Method method = this.getClass().getDeclaredMethod(m, HttpServletRequest.class, HttpServletResponse.class);
			// 执行方法
			method.invoke(this, req, resp);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
